/**
 * Copyright (C) 2010, 2011 Neofonie GmbH
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package eu.dicodeproject.blogimport.parser;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * String helper utility.
 * 
 * 
 * 
 */
public final class StringUtil {

	private static final Logger LOG = LoggerFactory.getLogger(StringUtil.class);

	private StringUtil() {
		// avoid instantiation
	}

	/**
	 * Converts {@link File} to {@link String}.
	 * 
	 * @param file
	 *            - File to read.
	 * @return String
	 * @throws IOException
	 */
	public static String readString(final File file) throws IOException {
		if (file == null) {
			throw new IOException("Null is not a valid file.");
		}
		FileReader fReader = null;
		BufferedReader bReader = null;
		StringBuffer buffer = new StringBuffer();
		try {
			fReader = new FileReader(file);
			bReader = new BufferedReader(fReader);
			String line = bReader.readLine();
			while (line != null) {
				buffer.append(line);
				line = bReader.readLine();
				if (line != null) {
					buffer.append("\n");
				}
			}

			bReader.close();
			fReader.close();
		} catch (IOException e) {
			LOG.error("Cant't read file" + file.getAbsolutePath());
			if (bReader != null) {
				bReader.close();
			}
			if (fReader != null) {
				fReader.close();
			}
			throw e;
		}
		return buffer.toString();
	}	
}
